package com.tour.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.tour.model.Member;

public class MemberAction extends BaseAction {

	private Integer memberId;
	private String loginName;
	private String trueName;
	private String password;
	private String gender;
	private String address;
	private String email;
	private String creditCard;
	private String tel;
	private String newPassword;

	public String home() throws Exception {
		return SUCCESS;
	}
	
	public String register() throws Exception {
		if (memberManager.findByLoginName(loginName).size() == 0) {
			Member member = new Member(loginName, trueName, password,
					gender, address, email, creditCard,	tel);
			memberManager.addMember(member);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

	public String updatePassword() throws Exception {
		Member member = memberManager.get(memberId);
		if (member != null) {
			member.setPassword(getNewPassword());
			memberManager.modifyMember(member);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
	
	public String login() throws Exception {
		ActionContext context = ActionContext.getContext();
		List<Member> members = memberManager.findByLoginName(loginName);
		if (members.size()!=0) {
			context.getSession().put("user", members.get(0));
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
	
	public String logout() throws Exception {
		ActionContext context = ActionContext.getContext();
		if(context.getSession().get("user") != null) {
			context.getSession().remove("user");
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
	
	public Integer getMemberId() {
		return memberId;
	}

	public void setMemberId(Integer memberId) {
		this.memberId = memberId;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getTrueName() {
		return trueName;
	}

	public void setTrueName(String trueName) {
		this.trueName = trueName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getCreditCard() {
		return creditCard;
	}

	public void setCreditCard(String creditCard) {
		this.creditCard = creditCard;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
}
